package com.yanqu.road.server.cmd.activity.dragonboatrace;

import com.yanqu.road.pb.dragonboatrace.DragonBoatRaceProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.dragonboatrace.DragonBoatRaceModule;
import com.yanqu.road.server.manger.activity.dragonboatrace.DragonBoatRaceMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_DRAGONBOATRACE_CLICK_DRUM, desc = "赛龙舟击鼓")
public class DragonBoatRaceClickDrumCmd extends AbstractCommand {


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ret = DragonBoatRaceMgr.getActivityEndStatus(player);
        if(ret != 0){
            return;
        }
        DragonBoatRaceModule dragonBoatRaceModule = DragonBoatRaceMgr.getDragonBoatRaceModule(player);
        if(dragonBoatRaceModule.dragonBoatRaceUser == null || dragonBoatRaceModule.dragonBoatRaceUser.startTime <= 0){
            ret = GameErrorCode.E_DRAGONBOATRACE_NOT_JOIN_GAME;
            return;
        }
        long curTime = System.currentTimeMillis();
        DragonBoatRaceProto.DragonBoatRaceClickDrumReqMsg reqMsg = DragonBoatRaceProto.DragonBoatRaceClickDrumReqMsg.parseFrom(packet.getBytes());
        if(dragonBoatRaceModule.dragonBoatRaceUser.curCalTime >= reqMsg.getTime() && reqMsg.getTime() > curTime){
            ret = GameErrorCode.E_DRAGONBOATRACE_TIME_ERROR;
            return;
        }
        if (reqMsg.getTime() >= dragonBoatRaceModule.dragonBoatRaceUser.endTime){
            ret = GameErrorCode.E_DRAGONBOATRACE_THE_GAME_IS_OVER;
            return;
        }
        if(reqMsg.getTime() - dragonBoatRaceModule.dragonBoatRaceUser.clickDrumLastTime < DragonBoatRaceMgr.getConfig().getDRAGONBOATRACE_CLICK_DRUM_CD()){
            ret = GameErrorCode.E_DRAGONBOATRACE_ACTIVITY_CD;
            return;
        }
        dragonBoatRaceModule.refreshGame(reqMsg.getTime());
        dragonBoatRaceModule.dragonBoatRaceUser.clickDrumLastTime = reqMsg.getTime();
        dragonBoatRaceModule.dragonBoatRaceUser.clickDrumNum++;
        DragonBoatRaceProto.DragonBoatRaceClickDrumRespMsg.Builder builder = DragonBoatRaceProto.DragonBoatRaceClickDrumRespMsg.newBuilder();
        builder.setRet(0);
        builder.setNodeInfo(DragonBoatRaceMgr.builderDragonBoatRaceNodeInfoEntity(dragonBoatRaceModule.dragonBoatRaceUser));
        player.sendPacket(ClientProtocol.U_DRAGONBOATRACE_CLICK_DRUM, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            DragonBoatRaceProto.DragonBoatRaceClickDrumRespMsg.Builder builder = DragonBoatRaceProto.DragonBoatRaceClickDrumRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_DRAGONBOATRACE_CLICK_DRUM, builder);
        }
    }
}
